//
//  UIButton+Extesion.swift
//  Teamo
//
//  Created by mdzz on 2021/12/3.
//
//  UIButton 扩展
//  可用于创建UIButton、设置不同状态、属性等

import UIKit



public extension UIButton {
    
    /// 创建文字Button
    /// - Parameters:
    ///   - frame: 位置大小
    ///   - title: 标题
    ///   - titleColor: 标题字体颜色
    ///   - font: 标题字体大小
    /// - Returns: UIButton
    @discardableResult
     class func titleButton(frame: CGRect = .zero, title: String = "", titleColor: UIColor = .clear) -> UIButton {
        let btn = UIButton(type: .custom)
        btn.frame = frame
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(titleColor, for: .normal)
        return btn
    }
    
    
    /// 创建图片Button
    /// - Parameters:
    ///   - frame: 位置大小
    ///   - image: 按钮图片
    /// - Returns: UIButton
    @discardableResult
     class func imageButton(frame: CGRect = .zero, image: UIImage?) -> UIButton{
        let btn = UIButton(type: .custom)
        btn.frame = frame
        btn.setImage(image, for: .normal)
        return btn
    }
    
    
    
    
    
     /// 设置图片
     /// - Parameters:
     ///   - image: 图片
     ///   - state: 状态
     /// - Returns: 返回自身
     @discardableResult
     func image(_ image: UIImage?, _ state: UIControl.State = .normal) -> Self {
         setImage(image, for: state)
         return self
     }
    
    
    
    /// 设置字体
    /// - Parameters:
    ///   - fontSize: 字体大小
    ///   - weight: 字重
    /// - Returns: 返回自身
    @discardableResult
    func font(_ fontSize: CGFloat, weight: UIFont.Weight = .regular) -> Self {
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize, weight: weight)
        return self
    }
    
}

public extension UIButton {
    
    /// 为不同state按钮设置背景色
    /// - Parameters:
    ///   - color: 背景色 UIColor
    ///   - state: state
    public func setBackgroundColor(color: UIColor, for state: UIControl.State) {
        let image = UIImage.image(with: color)
        self.setBackgroundImage(image, for: state)
    }
    
    

}


extension UIButton {
    
    /// 位置
    enum Position: Int {
        case top, bottom, leading, trailing
    }
    
    /// 设置按钮 图文间距 （需要提前设置按钮size）
    /// - Parameters:
    ///   - titlePosition: 文字位置
    ///   - space: 间距
    func set(titlePosition: Position = .leading, space: CGFloat = 0) {
        let imageSize = self.imageRect(forContentRect: self.frame).size
        let titleSize = self.titleRect(forContentRect: self.frame).size
        
        self.positionLabelRespectToImage(titleSize: titleSize, imageSize: imageSize, at: titlePosition, with: space)
    }

    private func positionLabelRespectToImage(titleSize: CGSize, imageSize: CGSize, at position: Position, with spacing: CGFloat) {
        switch (titleSize, imageSize, position) {
        case (.zero, _, _), (_, .zero, _):
            self.titleEdgeInsets = UIEdgeInsets.zero
            self.imageEdgeInsets = UIEdgeInsets.zero
            self.contentEdgeInsets = UIEdgeInsets.zero
        case (_, _, .top):
            self.titleEdgeInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing), left: -(imageSize.width), bottom: 0, right: 0)
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
            self.contentEdgeInsets = UIEdgeInsets(top: spacing / 2 + titleSize.height, left: -imageSize.width / 2, bottom: 0, right: -imageSize.width / 2)
        case (_, _, .bottom):
            self.titleEdgeInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing), left: -(imageSize.width), bottom: 0, right: 0)
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
            self.contentEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width / 2, bottom: spacing / 2 + titleSize.height, right: -imageSize.width / 2)
        case (_, _, .leading):
            if UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute) == .leftToRight {
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width * 2 + spacing))
                self.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: spacing / 2)
            } else {
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(imageSize.width * 2))
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -(titleSize.width * 2 + spacing), bottom: 0, right: 0)
                self.contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: 0)
            }
        case (_, _, .trailing):
            if UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute) == .leftToRight {
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                self.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: spacing / 2)
            } else {
                self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -spacing, bottom: 0, right: 0)
                self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                self.contentEdgeInsets = UIEdgeInsets(top: 0, left: spacing / 2, bottom: 0, right: 0)
            }
        }
    }

}
